<?php

namespace App\Http\Controllers;

use Cache;
use Illuminate\Http\Request;
use Searchy;
use Session;

class SearchController extends Controller
{
    public function search (Request $request) {
        $keyWord = $request->input('keyword');
        $keyWord = trim($keyWord);
        if (empty($keyWord)) {
            Session::flash('error', '搜索关键字不能空');
            return redirect(url()->previous());
        }
        $episodes = $value = Cache::remember('episode'.$keyWord, 30, function() use($keyWord) {
            return Searchy::episodes('raw_name')->query($keyWord)->getQuery()->having('relevance', '>', 30)->limit(100)->get();
        });
        return view('search.index', compact('episodes'));
    }

}
